home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ian & Stuart's Australian Mac: Not for Sale
/
Another.not.for.sale (Australia).iso
/
fade into you
/
getting there
/
Apps
/
MOO-1.7.6.src
/
src
/
objects.c
< prev
next >
Wrap
Text File
|
1994-11-02
|
15KB
|
612 lines
/******************************************************************************
Copyright (c) 1992 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
#include "db.h"
#include "exceptions.h"
#include "execute.h"
#include "functions.h"
#include "list.h"
#include "quota.h"
#include "server.h"
#include "storage.h"
#include "structures.h"
#include "utils.h"
struct bf_move_data {
Objid what, where;
};
static Var
make_arglist(Objid what)
{
Var r;
r = new_list(1);
r.v.list[1].type = TYPE_OBJ;
r.v.list[1].v.obj = what;
return r;
}
static package
do_move(Var arglist, Byte next, struct bf_move_data *data, Objid progr)
{
Objid what = data->what, where = data->where;
Objid oid, oldloc;
int accepts;
Var args;
enum error e;
switch (next) {
case 1: /* Check validity and decide `accepts' */
if (!valid(what) || (!valid(where) && where != NOTHING))
return make_error_pack(E_INVARG);
else if (obj_owner(what) != progr && !is_wizard(progr))
return make_error_pack(E_PERM);
else if (where == NOTHING)
accepts = 1;
else {
args = make_arglist(what);
e = call_verb(where, "accept", args, 0);
/* e will not be E_INVIND or E_TYPE */
if (e == E_NONE)
return make_call_pack(2, data);
else {
free_var(args);
if (e == E_MAXREC)
return make_error_pack(e);
else /* (e == E_VERBNF) */
accepts = 0;
}
}
goto accepts_decided;
case 2: /* Returned from `accepts' call */
accepts = is_true(arglist);
accepts_decided:
if (!is_wizard(progr) && accepts == 0)
return make_error_pack(E_NACC);
if (!valid(what)
|| (where != NOTHING && !valid(where))
|| obj_location(what) == where)
return no_var_pack();
/* Check to see that we're not trying to violate the hierarchy */
for (oid = where; oid != NOTHING; oid = obj_location(oid))
if (oid == what)
return make_error_pack(E_RECMOVE);
/* Remove thing from its old location, if it has one */
oldloc = obj_location(what);
if (valid(oldloc))
remove_content(oldloc, what);
/* Move it to the new location, if any */
if (valid(where))
add_content(where, what);
set_obj_location(what, where);
args = make_arglist(what);
e = call_verb(oldloc, "exitfunc", args, 0);
/* e != E_TYPE */
if (e == E_NONE)
return make_call_pack(3, data);
else {
free_var(args);
if (e == E_MAXREC)
return make_error_pack(e);
}
/* e == E_INVIND or E_VERBNF, in both cases fall through */
case 3: /* Returned from exitfunc call */
if (valid(where) && valid(what)
&& obj_location(what) == where) {
args = make_arglist(what);
e = call_verb(where, "enterfunc", args, 0);
/* e != E_TYPE or E_INVIND */
if (e == E_NONE)
return make_call_pack(4, data);
else {
free_var(args);
if (e == E_MAXREC)
return make_error_pack(e);
/* else e == E_VERBNF, fall through */
}
}
case 4: /* Returned from enterfunc call */
return no_var_pack();
default:
panic("Unknown PC in DO_MOVE");
return no_var_pack(); /* Dead code to eliminate compiler warning */
}
}
/* external */
static package
bf_toobj(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
enum error e;
r.type = TYPE_OBJ;
e = become_number(arglist.v.list[1], &(r.v.obj), 0);
free_var(arglist);
if (e != E_NONE)
return make_error_pack(e);
return make_var_pack(r);
}
static package
bf_typeof(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
r.type = TYPE_NUM;
r.v.num = (int) arglist.v.list[1].type;
free_var(arglist);
return make_var_pack(r);
}
static package
bf_create(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (parent [, owner]) */
Objid *data = vdata;
Var r;
if (next == 1) {
Objid parent, owner;
parent = arglist.v.list[1].v.obj;
owner = (arglist.v.list[0].v.num == 2
? arglist.v.list[2].v.obj
: progr);
free_var(arglist);
if (!valid(parent)
|| !Obj_perm(progr, parent, FLAG_FERTILE)
|| (owner != progr && !is_wizard(progr)))
return make_error_pack(E_PERM);
r.type = TYPE_OBJ;
r.v.obj = create_object("", parent, owner);
if (r.v.obj == NOTHING)
return make_error_pack(E_QUOTA);
else {
enum error e;
Var args;
data = alloc_data(sizeof(*data));
*data = r.v.obj;
args = new_list(0);
e = call_verb(r.v.obj, "initialize", args, 0);
/* e will not be E_INVIND or E_TYPE */
if (e == E_NONE)
return make_call_pack(2, data);
free_data(data);
free_var(args);
if (e == E_MAXREC)
return make_error_pack(e);
else /* (e == E_VERBNF) do nothing */
return make_var_pack(r);
}
} else { /* next == 2, returns from initialize verb_call */
r.type = TYPE_OBJ;
r.v.obj = *data;
free_data(data);
return make_var_pack(r);
}
}
static void
bf_create_write(void *vdata)
{
db_printf("bf_create data: oid = %d\n", *((Objid *) vdata));
}
static void *
bf_create_read(FILE *f)
{
Objid *data = alloc_data(sizeof(Objid));
if (fscanf(f, "bf_create data: oid = %d\n", data) == 1)
return data;
else
return 0;
}
static package
bf_chparent(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (object, new_parent) */
Var r;
r = change_parent(arglist.v.list[1].v.obj, arglist.v.list[2].v.obj, progr);
free_var(arglist);
if (r.type == TYPE_ERR)
return make_error_pack(r.v.err);
else
return no_var_pack();
}
static package
bf_valid(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (object) */
Var r;
r.type = TYPE_NUM;
r.v.num = valid(arglist.v.list[1].v.obj);
free_var(arglist);
return make_var_pack(r);
}
static package
bf_parent(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (object) */
Var r;
Objid obj = arglist.v.list[1].v.obj;
free_var(arglist);
if (!valid(obj))
return make_error_pack(E_INVARG);
else {
r.type = TYPE_OBJ;
r.v.obj = obj_parent(obj);
return make_var_pack(r);
}
}
static package
bf_children(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (object) */
Var r;
Objid obj = arglist.v.list[1].v.obj;
free_var(arglist);
if (!valid(obj))
return make_error_pack(E_INVARG);
else {
Var child;
Objid oid;
int i, count = 0;
for (oid = obj_child(obj);
oid != NOTHING;
oid = obj_sibling(oid))
count++;
r = new_list(count);
child.type = TYPE_OBJ;
for (oid = obj_child(obj), i = 1;
oid != NOTHING;
oid = obj_sibling(oid), i++) {
child.v.obj = oid;
r.v.list[i] = child;
}
return make_var_pack(r);
}
}
static package
bf_max_object(Var arglist, Byte next, void *vdata, Objid progr)
{ /* () */
Var r;
r.type = TYPE_OBJ;
r.v.obj = db_size() - 1;
free_var(arglist);
return make_var_pack(r);
}
static enum error
move_to_nothing(Objid oid, Byte func_pc, Objid *data)
{
/* All we need to do is the exitfunc and the remove_content */
Objid oldloc = obj_location(oid);
Var args;
enum error e;
if (valid(oldloc))
remove_content(oldloc, oid);
set_obj_location(oid, NOTHING);
args = new_list(1);
args.v.list[1].type = TYPE_OBJ;
args.v.list[1].v.obj = oid;
e = call_verb(oldloc, "exitfunc", args, 0);
/* e != E_TYPE */
if (e != E_NONE)
free_var(args);
return e;
}
static package
bf_recycle(Var arglist, Byte func_pc, void *vdata, Objid progr)
{ /* (object) */
Objid oid;
Var args;
enum error e;
Objid *data = vdata;
switch (func_pc) {
case 1:
oid = arglist.v.list[1].v.obj;
free_var(arglist);
if (!valid(oid))
return make_error_pack(E_INVARG);
if (!is_wizard(progr) && obj_owner(oid) != progr)
return make_error_pack(E_PERM);
data = alloc_data(sizeof(*data));
*data = oid;
args = new_list(0);
e = call_verb(oid, "recycle", args, 0);
/* e != E_INVIND or E_TYPE */
if (e == E_NONE)
return make_call_pack(2, data);
/* else e == E_VERBNF or E_MAXREC; fall through */
free_var(args);
goto moving_contents;
case 2: /* moving all contents to #-1 */
free_var(arglist);
oid = *data;
moving_contents:
if (!valid(oid)) {
free_data(data);
return no_var_pack();
}
while (obj_contents(oid) != NOTHING) {
if (move_to_nothing(obj_contents(oid), 2, data) == E_NONE)
return make_call_pack(2, data);
}
if (obj_location(oid) != NOTHING
&& move_to_nothing(oid, 2, data) == E_NONE)
/* Return to same case because this :exitfunc might add new */
/* contents to OID or even move OID right back in. */
return make_call_pack(2, data);
/* We can now be confident that OID has no contents and no location */
#ifdef MACINTOSH
/* Do the same thing for the inheritance hierarchy (much easier!) */
while (obj_child(oid) != NOTHING)
do_change_parent(obj_child(oid), obj_parent(oid));
#else
/* Do the same thing for the inheritance hierarchy (much easier!) */
while (obj_child(oid) != NOTHING)
(void) do_change_parent(obj_child(oid), obj_parent(oid));
#endif
if (valid(obj_parent(oid)))
remove_child(obj_parent(oid), oid);
/* Finish the demolition. */
destroy_object(oid);
free_data(data);
return no_var_pack();
}
panic("Can't happen in BF_RECYCLE");
return no_var_pack();
}
static void
bf_recycle_write(void *vdata)
{
Objid *data = vdata;
db_printf("bf_recycle data: oid = %d, cont = 0\n", *data);
}
static void *
bf_recycle_read(FILE *f)
{
Objid *data = alloc_data(sizeof(*data));
int junk;
if (fscanf(f, "bf_recycle data: oid = %d, cont = %d\n", data, &junk) == 2)
return data;
else
return 0;
}
static package
bf_players(Var arglist, Byte next, void *vdata, Objid progr)
{ /* () */
free_var(arglist);
return make_var_pack(var_ref(players_list));
}
static package
bf_is_player(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (object) */
Var r;
Objid oid = arglist.v.list[1].v.obj;
free_var(arglist);
if (!valid(oid))
return make_error_pack(E_INVARG);
r.type = TYPE_NUM;
r.v.num = is_player(oid);
return make_var_pack(r);
}
static package
bf_set_player_flag(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (object, yes/no) */
Var obj;
char bool;
obj = arglist.v.list[1];
bool = is_true(arglist.v.list[2]);
free_var(arglist);
if (!valid(obj.v.obj))
return make_error_pack(E_INVARG);
else if (!is_wizard(progr))
return make_error_pack(E_PERM);
if (bool) {
players_list = setadd(players_list, obj);
set_obj_flags(obj.v.obj, obj_flags(obj.v.obj) | (1 << FLAG_PLAYER));
} else {
boot_player(obj.v.obj);
players_list = setremove(players_list, obj);
set_obj_flags(obj.v.obj, obj_flags(obj.v.obj) & ~(1 << FLAG_PLAYER));
}
return no_var_pack();
}
static package
bf_move(Var arglist, Byte next, void *vdata, Objid progr)
{
struct bf_move_data *data = vdata;
package p;
if (next == 1) {
data = alloc_data(sizeof(*data));
data->what = arglist.v.list[1].v.obj;
data->where = arglist.v.list[2].v.obj;
}
p = do_move(arglist, next, data, progr);
free_var(arglist);
if (p.why != BI_CALL)
free_data(data);
return p;
}
static void
bf_move_write(void *vdata)
{
struct bf_move_data *data = vdata;
db_printf("bf_move data: what = %d, where = %d\n",
data->what, data->where);
}
static void *
bf_move_read(FILE *f)
{
struct bf_move_data *data = alloc_data(sizeof(struct bf_move_data));
if (fscanf(f, "bf_move data: what = %d, where = %d\n",
&(data->what), &(data->where)) == 2)
return data;
else
return 0;
}
void
register_objects(void)
{
(void) register_function("toobj", 1, 1, bf_toobj, TYPE_ANY);
(void) register_function("typeof", 1, 1, bf_typeof, TYPE_ANY);
(void) register_function_with_read_write("create", 1, 2, bf_create,
bf_create_read, bf_create_write,
TYPE_OBJ, TYPE_OBJ);
(void) register_function_with_read_write("recycle", 1, 1, bf_recycle,
bf_recycle_read, bf_recycle_write,
TYPE_OBJ);
(void) register_function("valid", 1, 1, bf_valid, TYPE_OBJ);
(void) register_function("parent", 1, 1, bf_parent, TYPE_OBJ);
(void) register_function("children", 1, 1, bf_children, TYPE_OBJ);
(void) register_function("chparent", 2, 2, bf_chparent,
TYPE_OBJ, TYPE_OBJ);
(void) register_function("max_object", 0, 0, bf_max_object);
(void) register_function("players", 0, 0, bf_players);
(void) register_function("is_player", 1, 1, bf_is_player, TYPE_OBJ);
(void) register_function("set_player_flag", 2, 2, bf_set_player_flag,
TYPE_OBJ, TYPE_ANY);
(void)register_function_with_read_write("move", 2, 2, bf_move,
bf_move_read, bf_move_write,
TYPE_OBJ, TYPE_OBJ);
}
char rcsid_objects[] = "$Id: objects.c,v 1.12 1992/10/23 23:03:47 pavel Exp $";
/* $Log: objects.c,v $
* Revision 1.12 1992/10/23 23:03:47 pavel
* Added copyright notice.
*
* Revision 1.11 1992/10/23 22:02:24 pavel
* Eliminated all uses of the useless macro NULL.
*
* Revision 1.10 1992/10/17 20:47:39 pavel
* Global rename of strdup->str_dup, strref->str_ref, vardup->var_dup, and
* varref->var_ref.
*
* Revision 1.9 1992/09/14 18:40:24 pjames
* Updated #includes. Moved rcsid to bottom.
*
* Revision 1.8 1992/09/14 17:32:23 pjames
* Moved db_modification code to db modules.
*
* Revision 1.7 1992/09/08 22:02:11 pjames
* Renamed bf_obj.c to objects.c
*
* Revision 1.6 1992/09/04 01:24:21 pavel
* Added support for the `f' (for `fertile') bit on objects.
*
* Revision 1.5 1992/09/03 16:32:24 pjames
* Free's propdefs using new array structure.
*
* Revision 1.4 1992/08/28 16:04:12 pjames
* Changed myfree(*, M_STRING) to free_str(*).
*
* Revision 1.3 1992/08/10 17:18:58 pjames
* Updated #includes. Move bf_pass to execute.c Updated to use new
* registration format. Built in functions now only receive programmer,
* instead of entire Parse_Info.
*
* Revision 1.2 1992/07/20 23:52:48 pavel
* Added rcsid_<filename-root> declaration to hold the RCS ident. string.
*
* Revision 1.1 1992/07/20 23:23:12 pavel
* Initial RCS-controlled version.
*/